excel超链接打开文件夹_Excel超链接运行命令文件

本文介绍了如何使用Excel超链接结合宏来运行命令文件,解决对话框弹出、无法添加命令行参数以及运行脚本文件的问题。作者J.Woolley提供了一种技术,当单元格内容包含'RUN::COMMAND'时,会触发宏ProcessRunCommand运行。同时,文章还揭示了一个Excel超链接的bug,以及如何避免这个问题。
摘要由CSDN通过智能技术生成

excel超链接打开文件夹

You can use Excel hyperlinks to navigate through a workbook, go to web pages, open other Excel files, and even open PDF files. However, if you try to use an Excel hyperlink to run command files, you'll run into problems. Until now! Here's how J.Woolley uses hyperlinks, with a background macro,  to avoid those problems – and a other problems too. He also found a strange hyperlink bug.

您可以使用Excel超链接浏览工作簿,转到网页,打开其他Excel文件,甚至打开PDF文件。 但是,如果您尝试使用Excel超链接来运行命令文件,则会遇到问题。 到现在! 这就是J.Woolley如何使用带有背景宏的超链接来避免这些问题以及其他问题。 他还发现了一个奇怪的超链接错误。

超链接命令文件问题 (Hyperlink Command File Problems)

Websites and Excel files open nicely (usually), if you click a hyperlink in a worksheet. With command files, you'll run into issues, such as:

如果您单击工作表中的超链接,则网站和Excel文件将正常打开(通常)。 使用命令文件,您会遇到一些问题,例如:

  • Dialog boxes appear, and you have to click an OK or Cancel button

    出现对话框,您必须单击“确定”或“取消”按钮
  • You can't add command line arguments in the hyperlink

    您不能在超链接中添加命令行参数
  • Shell window flashes with a long or complex script file (cmd or vbs)

    Shell窗口使用长脚本或复杂脚本文件(cmd或vbs)闪烁

地址错误问题 (Bad Address Problems)

Even with file types that should open from a hyperlink, you'll see an error message if there is a problem with the address. Here's what appears if I misspell Contextures in a cell, and then click the link that is automatically created.

即使应该从超链接打开的文件类型,如果地址有问题,您也会看到错误消息。 如果我在单元格中拼写了Contextures,然后单击自动创建的链接,则会出现以下内容。

The message says, "Unable to open http://www.contexturs.com/. Cannot locate the Internet server or proxy server.'

消息显示,“无法打开http://www.contexturs.com/。无法找到Internet服务器或代理服务器。”

Excel中,如果你想要通过宏命令批量下载图片,可以按照以下步骤操作: 1. **创建宏**: - 打开VBA编辑器(在Excel菜单栏中选择"开发者" -> "Visual Basic")。 - 在新的窗口中,点击"插入" -> "模块",然后在编辑区域编写以下代码: ```vba Sub DownloadImages() Dim rng As Range Dim imgURL As String Dim filename As String Dim i As Long '指定需要下载图片的范围 (这里假设是A1:B10) Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:A10") '替换为实际的单元格范围 For Each cell In rng If Not IsEmpty(cell.Value) Then '检查单元格是否包含链接 imgURL = cell.Value filename = Replace(imgURL, "http://", "") '去掉前缀,仅保留文件名部分 filename = Replace(filename, "/", "") '去掉斜杠 filename = Replace(filename, "?", "") '去掉问号 '下载图片到指定位置(这里假设保存在同一工作簿的某个文件) Call DownloadFile(imgURL, ThisWorkbook.Path & "\" & filename) End If Next cell '自定义下载函数 Sub DownloadFile(url As String, destPath As String) Dim objHttp As Object Set objHttp = CreateObject("MSXML2.XMLHTTP") objHttp.Open "GET", url, False '同步请求 objHttp.Send If objHttp.Status = 200 Then Open App.Path & destPath, 1, True '使用二进制模式打开文件 PutByte FileHandle, 1, objHttp.ResponseBody '开始写入数据 Close FileHandle MsgBox "Image downloaded successfully.", vbInformation Else MsgBox "Failed to download image from " & url & ". Error code: " & objHttp.Status, vbCritical End If End Sub End Sub ``` 2. **运行宏**: - 确认代码无误后,关闭VBA编辑器并返回Excel主界面。 - 按下`Alt + F8`唤起宏管理器,找到刚刚创建的`DownloadImages`宏,双击运行。 注意:这个示例是基础版本,实际应用时可能会遇到网络连接、权限等问题,需根据实际情况调整。另外,确保运行此宏时有足够的权限访问网络资源,并遵守版权政策。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值